package com.wyl.clinic.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.wyl.clinic.entity.Patient;
import com.wyl.clinic.common.QueryPageParam;
import com.wyl.clinic.common.Result;
import com.wyl.clinic.entity.User;
import com.wyl.clinic.enums.RoleEnum;
import com.wyl.clinic.model.PatientVo;
import com.wyl.clinic.service.PatientService;
import com.wyl.clinic.service.UserService;
import com.wyl.clinic.utils.Md5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 病人表 前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-02-20
 */
@RestController
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;
    @Autowired
    private UserService userService;

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        Page<User> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(User::getName,name);
        }
        lambdaQueryWrapper.eq(User::getRoleId, RoleEnum.PATIENT.getCode());
        List<PatientVo> patientVos = Lists.newArrayList();
        IPage<User> result = userService.page(page,lambdaQueryWrapper);
        List<User> users = result.getRecords();

        if (ObjectUtil.isNotEmpty(users)){
            List<Patient> patients = patientService.listByUserIds(users.stream().map(User::getId).collect(Collectors.toList()));
            Map<Integer, Patient> patientMap = patients.stream().collect(Collectors.toMap(Patient::getUserId, t -> t, (o1, o2) -> o1));
            users.forEach(user -> {
                PatientVo patientVo = new PatientVo();
                BeanUtils.copyProperties(user, patientVo);
                Patient patient = patientMap.get(user.getId());
                patientVo.setId(patient.getId());
                patientVo.setUserId(patient.getUserId());
                patientVo.setIdCard(patient.getIdCard());
                patientVo.setHeight(patient.getHeight());
                patientVo.setWeight(patient.getWeight());
                patientVo.setBlood(patient.getBlood());
                patientVos.add(patientVo);
            });
        }
        return Result.suc(patientVos,result.getTotal());
    }

    /**
     * 新增或更新
     * @param patientVo
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody PatientVo patientVo){
        Patient patient = patientService.getById(patientVo.getId());
        User user = userService.getById(patient.getUserId());
        BeanUtils.copyProperties(patientVo, user);
        user.setPassword(null);
        user.setId(patient.getUserId());
        userService.updateById(user);

        BeanUtils.copyProperties(patientVo, patient);
        patientService.updateById(patient);

        return Result.suc();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        Patient patient = patientService.getById(id);
        if (ObjectUtil.isEmpty(patient)){
            return Result.suc();
        }
        userService.removeById(patient.getUserId());
        patientService.removeById(id);
        return Result.suc();
    }

}
